﻿using DWH.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using DWH.Utils;

namespace RDTest
{


    /// <summary>
    ///This is a test class for DWHDBHelperTest and is intended
    ///to contain all DWHDBHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DWHDBHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion




        /// <summary>
        ///A test for GetPPTaskIDFromDB
        ///</summary>
        [TestMethod()]
        public void PushTaskToDBDBTest()
        {
            string taskName = Guid.NewGuid().ToString();            
            DWHDBHelper.PushTaskToDB(taskName);
        }

        [TestMethod()]
        [ExpectedException(typeof(UpdateException))]
        public void PushTaskToDBDBTestException()
        {
            string taskName = Guid.NewGuid().ToString();            
            DWHDBHelper.PushTaskToDB(taskName);

            DWHDBHelper.PushTaskToDB(taskName);
        }

        /// <summary>
        ///A test for GetPPTaskResumeData
        ///</summary>
        [TestMethod()]
        public void GetPPTaskResumeDataTest()
        {
            string taskName = "retest" + Guid.NewGuid().ToString();
            DWHDBHelper.PushTaskToDB(taskName);

            var data = DWHDBHelper.GetPPTaskResumeData(taskName);
            Assert.IsNull(data);

            Dictionary<string, string> newData = new Dictionary<string, string>();
            newData.Add("name", taskName);
            newData.Add("url", "http://www.bing.com");

            DWHDBHelper.UpdatePPTask(taskName, newData);

            Dictionary<string, string> getdata = DWHDBHelper.GetPPTaskResumeData(taskName);
            Assert.AreEqual(getdata.Count, 2);
            Assert.AreEqual(getdata["name"], taskName);
            Assert.AreEqual(getdata["url"], "http://www.bing.com");

            newData.Add("ext", "extdata");
            DWHDBHelper.UpdatePPTask(taskName, newData);
            Dictionary<string, string> getdata2 = DWHDBHelper.GetPPTaskResumeData(taskName);
            Assert.AreEqual(getdata2.Count, 3);
            Assert.AreEqual(getdata2["name"], taskName);
            Assert.AreEqual(getdata2["url"], "http://www.bing.com");
            Assert.AreEqual(getdata2["ext"], "extdata");
        }
               

        /// <summary>
        ///A test for AddPage
        ///</summary>
        [TestMethod()]
        public void AddPageTest()
        {
            string title = Guid.NewGuid().ToString();
            DWHDBHelper.PushTaskToDB(title);
            
            DWHDBHelper.AddPage(title, title.GetHashCode(), title, title, title + " content",DateTime.Now);
        }

        /// <summary>
        ///A test for GetPageContentForPush
        ///</summary>
        [TestMethod()]
        public void GetPageContentForPushTest()
        {
            string pullTaskName = "AICHMovieAsiaMSCBoardPullTask"; 
            int rowCount = 10;
            Dictionary<long, Page> expected = null;
            Dictionary<long, Page> actual;
            actual = DWHDBHelper.GetPageContentForPush("UT",pullTaskName, rowCount);            
        }

        /// <summary>
        ///A test for AddUndownloadedPageAttachment
        ///</summary>
        [TestMethod()]
        public void AddUndownloadedPageAttachmentTest()
        {
            string pageAttachmentUrl = "UT Test";
            int pageUrlHashCode = 733419595; 
            string attachmentType = "Image"; 
            string storedPath = @"E:\WebPPAttachment"; 
            DWHDBHelper.AddUndownloadedPageAttachment(pageAttachmentUrl, pageUrlHashCode, attachmentType, storedPath,DateTime.Now);           
        }

        /// <summary>
        ///A test for GetPageAttachmentByAttachmentType
        ///</summary>
        [TestMethod()]
        public void GetPageAttachmentByAttachmentTypeTest()
        {            
            List<PageAttachment> images = DWHDBHelper.GetPageAttachmentByAttachmentType(DWHConstants.ATTACHMENT_TYPE_IMAGE);

            List<PageAttachment> jiandowns = DWHDBHelper.GetPageAttachmentByAttachmentType(DWHConstants.ATTACHMENT_TYPE_TORRENT_JIANDOWN);      
        }
    }
}
